import * as THREE from "three";
// 相机
export default class {
  constructor(t) {
    var r = i.props.configuration;
    i.config = Object(w.reduceConfig)(r)
    i.childrenCommponentMap = new Map
    i.animate = i.animate.bind(u(i))
    i.currentScenarioManager = undefined
    i.addListener()
    this.initRender()
  }

  initRender() {
    var t = this.config.chart
    var e = t.dimension
    var n = t.cameraConfig
    var a = t.baseConfig
    var i = e.chartDimension
    var l = a.devicePixelRatio
    this.mapRender = new THREE.WebGLRenderer({
      alpha: true,
      antialias: true,
      logarithmicDepthBuffer: true
    })
    this.mapRender.setPixelRatio(l || window.devicePixelRatio)
    this.mapRender.setSize(i.width, i.height)
    this.mapContainer.appendChild(this.mapRender.domElement)
    this.scene = new THREE.Scene()
    this.camera = new THREE.PerspectiveCamera()
    this.configCamera(i.width, i.height, 45, this.getCameraPosition(n))
    this.control = new y.OrbitControls(this.camera, this.mapRender.domElement)
    this.control.enableDamping = !0
    this.control.enableKeys = !1
    this.control.minDistance = 1010
    this.control.maxDistance = 1e5
    this.earthGroup = new y.Group
    this.scene.add(this.earthGroup)
    this.otherGroup = new y.Group
    this.scene.add(this.otherGroup)
    var s = this.config.basePlane.scenes
    u = Object.values(s)[0];
    n && this.animateScene(u)
  }
  getCameraPosition(t) {
    var e = t.viewPoint
    var n = 1010 + t.distance / 100 * 98990
    var a = S(e.lng, e.lat, n);
    return new y.Vector3(a[0], a[1], a[2])
  }
  configCamera(t, e, n, a) {
    this.camera.aspect = t / e
    this.camera.fov = n
    this.camera.near = 1e-4
    this.camera.far = 1e5
    this.camera.position.x = a.x
    this.camera.position.y = a.y
    this.camera.position.z = a.z
    this.camera.updateProjectionMatrix()
  }
  getCamera() {
    var t = new THREE.Vector3()
    this.control && t.copy(this.control.target)
    return {
      target: {
        x: t.x,
        y: t.y,
        z: t.z
      },
      position: {
        x: this.camera.position.x,
        y: this.camera.position.y,
        z: this.camera.position.z
      }
    }
  }
  cameraRotate(t) {
    if (this.control) {
      this.control.autoRotate = true
    }
    if (t) {
      this.control.autoRotateSpeed = t
    }
  }
  stopFly() {
    if (this.tween_fly) {
      this.tween_fly.stop()
      this.tween_fly = null
    }
  }
  pauseFly() {
    if (this.tween_fly) {
      this.tween_fly.pause()
    }
  }
  resumeFly() {
    if (this.tween_fly) {
      this.tween_fly.resume()
    }
  }
  stopRotate() {
    if (this.control) {
      this.control.autoRotate = false
    }
  }
  animateScene(t) {
    var e = this;
    if (t) {
      this.stopFly()
      this.stopRotate();
      var n = t.open
        , a = t.type
        , i = t.camera
        , r = t.entranceTime
        , o = t.rotateCamera
        , l = t.speed
        , s = t.flyCameras
        , u = t.loop;
      !r && (r = 1),
        n ? "rotate" == a ? this.cameraFly({
          position: o.position,
          target: o.target,
          time: r,
          callback: function () {
            e.cameraRotate(l)
          }
        }) : this.cameraFly({
          position: s[0].position,
          target: s[0].target,
          time: r,
          callback: function () {
            e.cameraCruise({
              cameras: s,
              loop: u,
              index: 1
            })
          }
        }) : this.cameraFly({
          position: i.position,
          target: i.target,
          time: r
        })
    }
  }
  componentWillUnmount() {
    this.timer && cancelAnimationFrame(this.timer)
    this.timer = null
    this.scene.remove(this.earthGroup)
    this.scene.remove(this.otherGroup)
    this.scene = null
    this.earthGroup = null
    this.otherGroup = null
    this.control && this.control.dispose()
    this.control = null
    this.camera = null
    this.mapRender.dispose()
    this.mapRender = null
    this.destroyChildren()
    this.removeListener()
  }
  cameraFly (t) {
    
  }
  cameraCruise (t) {
    
  }
}